package com.wgts.wgts.mvp.presenter.imp;

import com.wgts.wgts.mvp.model.IBindPhoneActivityModel;
import com.wgts.wgts.mvp.model.ISendSMSCodeModel;
import com.wgts.wgts.mvp.presenter.IBindPhoneActivityPersenter;
import com.wgts.wgts.mvp.view.IBindPhoneActivityView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.req.SmsCodeRequest;
import com.wgts.wgts.netmodle.req.ThridPlatFromRequest;
import com.wgts.wgts.netmodle.resp.CurrentEntity;
import com.wgts.wgts.netmodle.resp.LoginEntity;
import com.wgts.wgts.netmodle.resp.ThridPreBindEntity;
import com.wgts.wgts.react.MD5Util;
import com.wgts.wgts.rxjava.MVPResultSubscriber;

public class BindPhoneActivityPersenter implements IBindPhoneActivityPersenter {

    private IBindPhoneActivityModel model;

    private IBindPhoneActivityView view;

    private ISendSMSCodeModel smsCodeModel;

    public static final int SMS_ERROR = 011;

    public BindPhoneActivityPersenter(IBindPhoneActivityModel model, IBindPhoneActivityView view, ISendSMSCodeModel sendSMSCodeModel) {
        this.model = model;
        this.view = view;
        this.smsCodeModel = sendSMSCodeModel;
    }

    @Override
    public void bindUser(String userYqCode, String uid, String smsCode, String phone, String unionid) {
        ThridPlatFromRequest request = new ThridPlatFromRequest();
        request.phone = phone;
        request.smsCode = smsCode;
        request.uid = uid;
        request.userYQCode = userYqCode;
        request.unionid = unionid;
        model.bindUser(request, new HttpResultSubscriber<LoginEntity>() {
            @Override
            public void onSuccess(LoginEntity loginEntity) {
                //view.loginResult(loginEntity);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void sendSms(String phone, String scene) {
        SmsCodeRequest smsCodeRequest = new SmsCodeRequest();
        smsCodeRequest.scene = scene;
        smsCodeRequest.phone = phone;
        smsCodeModel.sendSMS(smsCodeRequest, new HttpResultSubscriber<CurrentEntity>() {
            @Override
            public void onSuccess(CurrentEntity currentEntity) {
                view.sendSmsResult();
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(SMS_ERROR, msg);
            }
        });
    }

    @Override
    public void addCountDownTimeListener() {
        smsCodeModel.setCutDownTime(new MVPResultSubscriber<Integer>() {
            @Override
            public void netx(Integer integer) {
                if(integer == 0){
                    view.countDownTimeOver();
                    return;
                }
                view.countDowmTime(integer);
            }
        });
    }

    @Override
    public void removeCountDownTimeListener() {
        smsCodeModel.removeCutDownTime();
    }

    @Override
    public void thridPreBind(String thirdparty, String uid, String unionid, String phone, String smsCode) {
        model.thridPreBind(thirdparty, uid, unionid, phone, smsCode, new HttpResultSubscriber<ThridPreBindEntity>() {
            @Override
            public void onSuccess(ThridPreBindEntity thridPreBindEntity) {
                view.loginResult(thridPreBindEntity);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void register(String username, String password, String smsCode, String inviteCode) {
        password = MD5Util.encrypt(password);
        model.register(username, password, smsCode, inviteCode, new HttpResultSubscriber<LoginEntity>() {
            @Override
            public void onSuccess(LoginEntity loginEntity) {
                view.registerResult(loginEntity);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }
}
